package benny_driving_ser.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import atg.taglib.json.util.JSONArray;
import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.dao.impl.LocationShareDaoImpl;
import benny_driving_ser.entity.DriLocation;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.util.SessionUtil;

public class LocationShareServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3553377810076217895L;

	/**
	 * Constructor of the object.
	 */
	public LocationShareServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action;
		if (null == (action = request.getParameter("action")))
			return;
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		if ("dri-dwgx".equals(action.trim())) {
			String log, lat;
			if (null == (log = request.getParameter("log"))
					|| null == (lat = request.getParameter("lat")))
				return;
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			if (null != sad) {
				int res = LocationShareDaoImpl.getInstance().shareLocation(
						sad.getValue().trim(), log.trim(), lat.trim());
				out.print(res);
			} else
				out.print(-1);
		}

		if ("dri-qxdwgx".equals(action.trim())) {
//			String driid;
//			if (null == (driid = request.getParameter("driid")))
//				return;
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			if(null!=sad){
				int res = LocationShareDaoImpl.getInstance().clearLocData(
						Integer.parseInt(sad.getValue()));
				out.print(res);
			}else
				out.print(-1);
		}

		if ("hqdw".equals(action.trim())) {
			String log, lat;
			if (null == (log = request.getParameter("log"))
					|| null == (lat = request.getParameter("lat")))
				return;
			List<DriLocation> res = LocationShareDaoImpl.getInstance()
					.getDriInRange(log.trim(), lat.trim());
			if (res != null) {
				JSONObject jo = new JSONObject();
				JSONArray ja = new JSONArray();
				try {
					for (DriLocation driloc : res) {
						JSONObject mJo = new JSONObject();
						mJo.put("driid", driloc.getDriid());
						mJo.put("state", driloc.getState());
						mJo.put("stars", driloc.getStars());
						mJo.put("log", driloc.getLog());
						mJo.put("lat", driloc.getLat());
						mJo.put("dis", driloc.getDis());
						mJo.put("addtime", driloc.getAddtime());
						ja.add(mJo);
					}
					jo.put("drilocs", ja);
					out.print(jo.toString());
				} catch (JSONException e) {
					e.printStackTrace();
					out.print("{'drilocs':[]}");
				}
			}
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
